package org.yn.order.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.yn.aftermarket.mapper.CustomerMapper;
import org.yn.emp.domain.Employee;
import org.yn.finance.domain.Receivables;
import org.yn.finance.domain.ReceivablesItem;
import org.yn.finance.mapper.ReceivablesItemMapper;
import org.yn.finance.mapper.ReceivablesMapper;
import org.yn.order.domain.Contract;
import org.yn.order.domain.Order;
import org.yn.order.dto.PaymentDTO;
import org.yn.order.mapper.ContractMapper;
import org.yn.order.mapper.OrderMapper;
import org.yn.order.service.IOrderService;
import org.yn.base.service.impl.BaseServiceImpl;
import org.springframework.stereotype.Service;
import org.yn.prod.mapper.ProductMapper;

import java.math.BigDecimal;
import java.util.Calendar;
import java.util.Date;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author yn
 * @since 2023-08-02
 */
@Service
public class OrderServiceImpl extends BaseServiceImpl<Order> implements IOrderService {

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private ContractMapper contractMapper;

    @Autowired
    private ProductMapper productMapper;

    @Autowired
    private CustomerMapper customerMapper;

    @Autowired
    private ReceivablesMapper receivablesMapper;

    @Autowired
    private ReceivablesItemMapper receivablesItemMapper;

    @Override
    public BigDecimal getPaymentPrice(Long id) {
        // 根据订单id获取
        // 获取定金
       BigDecimal deposit =  orderMapper.selectDeposit(id);

        // 获取产品金额
        BigDecimal productPrice= orderMapper.selectProductPrice(id);
        return productPrice.subtract(deposit);
    }

    @Override
    public void payment(PaymentDTO paymentDTO, Employee loginUser) {
        //修改订单状态
        Order order = orderMapper.selectById(paymentDTO.getId());
        order.setState(1);
        orderMapper.update(order);


        // 创建应收款表明细
        // 创建应收款表
        Receivables receivables = new Receivables();
        receivables.setCustomerId(order.getCustomerId());
        receivables.setCustomerName(order.getCustomerName());
        receivables.setPaymentMethodId(paymentDTO.getPaymentMethod().getId());
        receivables.setProductId(order.getProductId());
        receivables.setProductName(order.getProductName());
        receivables.setPrice(getPaymentPrice(order.getId()));
        // 查询合同
        Contract contract = contractMapper.selectByOrderId(order.getId());
        receivables.setContractId(contract.getId());
        receivablesMapper.insert(receivables);
        // 创建保修单
        // 创建应收款明细表
        // 明细的数量是要跟着 支付方式判断来的
        String value = paymentDTO.getPaymentMethod().getValue();
        int i = Integer.parseInt(value);
        ReceivablesItem receivablesItem = new ReceivablesItem();
        receivablesItem.setContractId(contract.getId());
        receivablesItem.setCustomerId(order.getCustomerId());
        receivablesItem.setCustomerName(order.getCustomerName());
        receivablesItem.setName(order.getBusinessName());
        receivablesItem.setPayee(loginUser.getId());
        receivablesItem.setPayeeName(loginUser.getUsername());
        receivablesItem.setReceivablesId(receivables.getId());
        receivablesItem.setState(0);
        receivablesItem.setProductId(order.getProductId());
        receivablesItem.setProductName(order.getProductName());

        if (i > 0){
            // 多条数据
            i *= 12;
            BigDecimal divide = receivables.getPrice().divide(new BigDecimal(i),2,BigDecimal.ROUND_CEILING);
            receivablesItem.setPrice(divide);
            Calendar instance = Calendar.getInstance();
            for (int j = 0; j < i; j++) {
                instance.add(Calendar.MONTH,1);
                int i1 = instance.get(Calendar.MONTH);

                receivablesItem.setLastShouldPayTime(instance.getTime());
                saveReceivablesItem(receivablesItem);


            }

        }else {
            // 一条数据

            receivablesItem.setPrice(receivables.getPrice());
            receivablesItem.setLastShouldPayTime(new Date());
            saveReceivablesItem(receivablesItem);
        }
    }

    private void saveReceivablesItem( ReceivablesItem receivablesItem) {

        receivablesItemMapper.insert(receivablesItem);
    }
}
